package cn.b.sky.mes.em.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.em.model.EquipmentScheduledTime;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.mes.em.model.condition.EquipmentScheduledTimeCondition;
import cn.b.sky.mes.em.service.EquipmentScheduledTimeService;
import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.model.DWZResult;
import org.apache.commons.lang3.StringUtils;
import cn.b.sky.user.SecurityContextUtil;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : EquipmentScheduledTimeController.java<br>
 * </strong> <strong>Package : cn.b.sky.mes.em.controller<br>
 * </strong> <strong>Description : </strong>设备计划作业时长控制器<br>
 * <strong>Create on : 2015-11-18 下午03:40:06<br>
 * </strong>
 * <p>
 * 
 * @author zhaoxin01<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 *          <strong>修改历史:</strong><br>
 *          修改人 | 修改日期 | 修改描述<br>
 *          -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping(value = "/equipmentScheduledTime/*")
public class EquipmentScheduledTimeController extends SecurityController {
  @Resource
  EquipmentScheduledTimeService equipmentScheduledTimeService;

  @RequestMapping("scheduledTimeList")
  public String scheduledTimeList(Model model, @ModelAttribute("condition") EquipmentScheduledTimeCondition condition,
      String navTabId) {
    condition.setSiteCode(SecurityContextUtil.getSiteCode());
    if (StringUtils.isEmpty(condition.getOrderField())) {
      condition.setOrderField("createDateTime");
    }
    List<EquipmentScheduledTime> scheduledTimeList = equipmentScheduledTimeService
        .selectEquipmentScheduledTimeByCondition(condition);
    int count = equipmentScheduledTimeService.selectEquipmentScheduledTimeCount(condition);
    condition.setData(scheduledTimeList);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "em/scheduledTime/scheduledTime_list";
  }

  @RequestMapping("addScheduledTime")
  public String addScheduledTime(Model model, String navTabId) {
    model.addAttribute(NAVTABID, navTabId);
    return "em/scheduledTime/scheduledTime_add";
  }

  @RequestMapping("modifyScheduledTime")
  public String modifyScheduledTime(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    EquipmentScheduledTime scheduledTime = equipmentScheduledTimeService.selectEquipmentScheduledTimeById(Long
        .parseLong(id));
    model.addAttribute("scheduledTime", scheduledTime);
    return "em/scheduledTime/scheduledTime_edit";
  }

  @RequestMapping("viewScheduledTime")
  public String viewScheduledTime(Model model, String navTabId, String id) {
    model.addAttribute(NAVTABID, navTabId);
    EquipmentScheduledTime scheduledTime = equipmentScheduledTimeService.selectEquipmentScheduledTimeById(Long
        .parseLong(id));
    model.addAttribute("scheduledTime", scheduledTime);
    return "em/scheduledTime/scheduledTime_view";
  }

  @RequestMapping("saveScheduledTime")
  @ResponseBody
  public DWZResult saveScheduledTime(Model model, String navTabId, EquipmentScheduledTime scheduledTime, String id,
      HttpServletRequest request) {
    if (scheduledTime == null) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }
    scheduledTime.setSiteCode(getSiteCode());
    setCreateInfo(scheduledTime);
    if (StringUtils.isEmpty(id)) {
      if (equipmentScheduledTimeService.selectEquipmentScheduledTimeCountByDate(scheduledTime) > 0) {
        return ajaxError(getImeViewMessageI18n(null,
            "scheduledTimeExist.M"));
      }
      equipmentScheduledTimeService.insertEquipmentScheduledTime(scheduledTime);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);
    } else {
      equipmentScheduledTimeService.updateEquipmentScheduledTime(scheduledTime);
      return dialogAjaxDone(
          getImeViewMessageI18n(null, "successfulOperation.M"),
          null, null, null, null);

    }
  }

  @RequestMapping("deleteScheduledTime")
  @ResponseBody
  public DWZResult deleteScheduledTime(Model model, String navTabId, String id, HttpServletRequest request) {
    if (StringUtils.isEmpty(id)) {
      return ajaxError(getImeViewMessageI18n(null,
          SystemConstant.PARAMETERNULL));
    }

    String ids = request.getParameter("ids");
    String[] ress = ids.split(",");
    for (String res : ress) {
      equipmentScheduledTimeService.deleteEquipmentScheduledTime(Long.parseLong(res));
    }
    return navTabAjaxDone(navTabId, null, null, null,
        getImeViewMessageI18n(null, "successfulOperation.M"),
        null);

  }
}
